package com.genesis.yunnanji.utils;

import android.text.TextUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 时间转换工具
 */
public class DateUtil {
    /**
     * 一秒毫秒数
     */
    public static final int MILLISECOND_OF_SECOND = 1000;
    /**
     * 一分钟毫秒数
     */
    public static final int MILLISECOND_OF_MINUTE = 60 * 1000;
    /**
     * 一小时毫秒数
     */
    public static final int MILLISECOND_OF_HOUR = 60 * 60 * 1000;
    /**
     * 一天毫秒数
     */
    public static final int MILLISECOND_OF_DAY = 24 * 60 * 60 * 1000;

    /**
     * 一分钟秒数
     */
    public static final int SECOND_OF_MINUTE = 60;
    /**
     * 一小时秒数
     */
    public static final int SECOND_OF_HOUR = 60 * 60;
    /**
     * 一天秒数
     */
    public static final int SECOND_OF_DAY = 24 * 60 * 60;

    /**
     * String串转Date
     *
     * @param DateString 被转换的时间字符串
     * @param format     被转换的时间字格式
     * @return 返回转换后的时间
     */
    public static Date String2Date(String DateString, String format) {
        if (TextUtils.isEmpty(DateString)) {
            return null;
        }
        DateFormat df = new SimpleDateFormat(format, Locale.getDefault());
        try {
            return df.parse(DateString);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Date转String
     *
     * @param date   被转换的时间
     * @param format 被转换的时间字格式
     * @return 返回转换后的时间字符串
     */
    public static String Date2String(Date date, String format) {
        DateFormat df = new SimpleDateFormat(format, Locale.getDefault());
        try {
            return df.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 时间字符串转换成新时间字符串
     *
     * @param fromDateString 被转换的时间字符串
     * @param fromDateFormat 被转换的时间字格式
     * @param toDateFormat   目标时间格式
     * @return String 返回转换后的时间字符串
     */
    public static String StringToString(String fromDateString, String fromDateFormat, String toDateFormat) {
        Date date = String2Date(fromDateString, fromDateFormat);
        if (date != null) {
            return Date2String(date, toDateFormat);
        }
        return null;
    }


    /**
     * 格式化时间戳时间
     *
     * @param dataFormat 格式化字符串
     * @param timeStamp  时间戳时间
     * @return
     */
    public static String formatDataByTimestamp(String dataFormat, long timeStamp) {
        if (timeStamp == 0) {
            return "";
        }
        String result;
        SimpleDateFormat format = new SimpleDateFormat(dataFormat);
        result = format.format(new Date(timeStamp * 1000));
        return result;
    }

    /**
     * 格式化毫秒时间
     *
     * @param dataFormat  格式化字符串
     * @param millisecond 毫秒时间
     * @return
     */
    public static String formatDataByMillisecond(String dataFormat, long millisecond) {
        if (millisecond == 0) {
            return "";
        }
        String result;
        SimpleDateFormat format = new SimpleDateFormat(dataFormat);
        result = format.format(new Date(millisecond));
        return result;
    }

    /**
     * Date转相对于今天的String
     *
     * @param date    被转换的时间
     * @param nowDate 今天
     * @return
     */
    public static String Date2StringFromNow(Date date, Date nowDate) {
        String result = null;
        if (date != null) {
            if (nowDate == null) {
                nowDate = new Date();
            }
            Calendar time = Calendar.getInstance();
            time.setTime(date);
            long tb1 = time.getTimeInMillis();
            time.set(Calendar.HOUR_OF_DAY, 0);
            time.set(Calendar.MINUTE, 0);
            time.set(Calendar.SECOND, 0);
            time.set(Calendar.MILLISECOND, 0);
            long tb2 = time.getTimeInMillis();
            time.setTime(nowDate);
            long tn1 = time.getTimeInMillis();
            time.set(Calendar.HOUR_OF_DAY, 0);
            time.set(Calendar.MINUTE, 0);
            time.set(Calendar.SECOND, 0);
            time.set(Calendar.MILLISECOND, 0);
            long tn2 = time.getTimeInMillis();
            int interval = (int) ((tn2 - tb2) / MILLISECOND_OF_DAY);
            if (interval == 1) {
                result = "昨天";
            } else if (interval == 2) {
                result = "前天";
            } else if (interval > 0) {
                result = Date2String(date, "MM-dd");
            } else {
                long distance = tn1 - tb1;
                if (distance < 5 * MILLISECOND_OF_MINUTE) {
                    result = "刚刚";
                } else if (distance < MILLISECOND_OF_HOUR) {
                    interval = (int) (distance / (MILLISECOND_OF_MINUTE));
                    result = interval + "分钟前";
                } else {
                    interval = (int) (distance / (MILLISECOND_OF_HOUR));
                    result = interval + "小时前";
                }
            }
        }
        return result;
    }

    /**
     * Date转相对于今天的String
     *
     * @param date 被转换的时间
     * @return
     */
    public static String Date2StringFromNowTime(long date) {
        String result = null;
        try {
            long now = System.currentTimeMillis();
            long diff = now - date;
            long interval = diff / MILLISECOND_OF_DAY;
            if (interval >= 30) {
                result = Date2String(new Date(date), "MM-dd");
            } else if (interval >= 1 && interval < 30) {
                result = interval + "天前";
            } else {
                if (diff < MILLISECOND_OF_MINUTE) {
                    result = "刚刚";
                } else if (diff < MILLISECOND_OF_HOUR) {
                    interval = (int) (diff / (MILLISECOND_OF_MINUTE));
                    result = interval + "分钟前";
                } else {
                    interval = (int) (diff / (MILLISECOND_OF_HOUR));
                    result = interval + "小时前";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * Date转相对于今天的String
     *
     * @param
     * @return
     */
    public static String Date2StringFromNowTime(long current, long before) {
        String result = null;
        try {
            long diff = current - before;
            long interval = diff / MILLISECOND_OF_DAY;
            if (interval >= 30) {
                result = Date2String(new Date(before), "MM-dd");
            } else if (interval >= 1 && interval < 30) {
                result = interval + "天前";
            } else {
                if (diff < MILLISECOND_OF_MINUTE) {
                    result = null;
                } else if (diff < MILLISECOND_OF_HOUR) {
                    interval = (int) (diff / (MILLISECOND_OF_MINUTE));
                    if (interval < 5) {
                        result = null;
                    } else {
                        result = interval + "分钟前";
                    }
                } else {
                    interval = (int) (diff / (MILLISECOND_OF_HOUR));
                    result = interval + "小时前";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String TimeStamp2String(long time) {
        String result;
        int interval = (int) (time / SECOND_OF_DAY);
        if (interval > 0) {
            result = interval + "天前";
        } else {
            interval = (int) (time / SECOND_OF_HOUR);
            if (interval > 0) {
                result = interval + "小时前";
            } else {
                interval = (int) (time / SECOND_OF_MINUTE);
                if (interval > 0) {
                    result = interval + "分钟前";
                } else if (time > MILLISECOND_OF_SECOND) {
                    result = time / MILLISECOND_OF_SECOND + "秒前";
                } else {
                    result = "1秒前";
                }
            }
        }
        return result;
    }

    public static String getChatTimeString(Date date) {
        long time = date.getTime();
        Calendar calendar = Calendar.getInstance();
        long days = DateUtil.getDays(date, calendar.getTime());
        if (days >= 2) {
            return DateUtil.formatDataByMillisecond("MM月dd日 HH:mm", time);
        } else if (days == 1) {
            return DateUtil.formatDataByMillisecond("昨天 HH:mm", time);
        } else {
            return DateUtil.formatDataByMillisecond("HH:mm", time);
        }

    }

    /**
     * 获取两个时间相差天数
     *
     * @param date1 Date
     * @param date2 Date
     * @return
     */
    public static long getDays(Date date1, Date date2) {
        try {
            Calendar c = Calendar.getInstance();

            c.setTime(date1);
            c.set(Calendar.HOUR_OF_DAY, 0);
            c.set(Calendar.MINUTE, 0);
            c.set(Calendar.SECOND, 0);
            c.set(Calendar.MILLISECOND, 0);
            long l1 = c.getTimeInMillis();

            c.setTime(date2);
            c.set(Calendar.HOUR_OF_DAY, 0);
            c.set(Calendar.MINUTE, 0);
            c.set(Calendar.SECOND, 0);
            c.set(Calendar.MILLISECOND, 0);
            long l2 = c.getTimeInMillis();

            long quot = 0;
            quot = l2 - l1;
            quot = quot / 1000 / 60 / 60 / 24;

            return quot;
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 获取两个时间相差年数
     *
     * @param date1 Date
     * @param date2 Date
     * @return
     */
    public static int getYears(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            return 0;
        }
        try {
            Calendar c = Calendar.getInstance();

            c.setTime(date1);

            int year1 = c.get(Calendar.YEAR);

            c.setTime(date2);

            int year2 = c.get(Calendar.YEAR);

            return year1 - year2;
        } catch (Exception e) {
            return 0;
        }
    }

    public static String millis2Until(long mss) {
        long days = mss / MILLISECOND_OF_DAY;
        long remainder = mss % MILLISECOND_OF_DAY;
        if (days > 0) {
            if (remainder > 0) {
                days++;
            }
            return days + "天";
        }
        long hours = (mss % MILLISECOND_OF_DAY) / MILLISECOND_OF_HOUR;
        remainder = (mss % MILLISECOND_OF_DAY) % MILLISECOND_OF_HOUR;
        if (hours > 0) {
            if (remainder > 0) {
                hours++;
            }
            return hours + "小时";
        }
        long minutes = (mss % MILLISECOND_OF_HOUR) / MILLISECOND_OF_MINUTE;
        remainder = (mss % MILLISECOND_OF_HOUR) / MILLISECOND_OF_MINUTE;
        if (minutes > 0) {
            if (remainder > 0) {
                minutes++;
            }
            return minutes + "分钟";
        }
        long seconds = (mss % MILLISECOND_OF_MINUTE) / MILLISECOND_OF_SECOND;
        return seconds + "秒";
    }

    public static String millis2Time(long mss) {
        String result;
        long interval;
        if (mss < MILLISECOND_OF_MINUTE) {
            interval = mss / MILLISECOND_OF_SECOND;
            result = interval + "秒";
        } else if (mss < MILLISECOND_OF_HOUR) {
            interval = (int) (mss / (MILLISECOND_OF_MINUTE));
            result = interval + "分钟";
        } else {
            interval = (int) (mss / (MILLISECOND_OF_HOUR));
            result = interval + "小时";
        }
        return result;
    }


    public static String millis2CountDown(long mss, boolean showMillis) {
        StringBuilder sBuilder = new StringBuilder();
        long days = mss / MILLISECOND_OF_DAY;
        long hours = (mss % MILLISECOND_OF_DAY) / MILLISECOND_OF_HOUR;
        long minutes = (mss % MILLISECOND_OF_HOUR) / MILLISECOND_OF_MINUTE;
        long seconds = (mss % MILLISECOND_OF_MINUTE) / MILLISECOND_OF_SECOND;
        long millis = mss % MILLISECOND_OF_SECOND;

        if (days >= 10) {
            sBuilder.append(days).append("天 ");
        } else if (days > 0) {
            sBuilder.append(days).append("天 ");
        }

        if (hours >= 10) {
            sBuilder.append(hours);
        } else {
            sBuilder.append("0").append(hours);
        }

        sBuilder.append(":");

        if (minutes >= 10) {
            sBuilder.append(minutes);
        } else {
            sBuilder.append("0").append(minutes);
        }
        sBuilder.append(":");

        if (seconds >= 10) {
            sBuilder.append(seconds);
        } else {
            sBuilder.append("0").append(seconds);
        }
        if (showMillis) {
            sBuilder.append(":");
            if (millis >= 100) {
                sBuilder.append(millis);
            } else if (millis >= 10) {
                sBuilder.append("0").append(millis);
            } else {
                sBuilder.append("00").append(millis);
            }
        }
        return sBuilder.toString();
    }

    /**
     * @param millisecond 毫秒
     * @return mm:ss格式时间
     */
    public static String getTimeText(long millisecond) {
        int second = (int) (millisecond / 1000);
        int minute = second / 60;
        second = second - minute * 60;
        return (minute > 9 ? String.valueOf(minute) : "0" + minute) + ":" + (second > 9 ? String.valueOf(second) : "0" + second);
    }
}
